//
//  FSCMessageViewModel.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/30.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCMessageViewModel {

    //当前的消息模型
    var message:FSCMessageModel {
        didSet {
            if message.likenum > 0 {
                AttributeLikeStr = getLikeNameWithAttribute()
                
            }
            setReplayListAtttibuteStr()
        }
    }
    //点赞的富文本属性
    var AttributeLikeStr = NSAttributedString()
    //回复列表内容的富文本属性数组
    var replayListAttributeArr = [NSAttributedString]()
    //当前回复视图的高度
    var replayListViewH:CGFloat = 0
    
    
    init(model:FSCMessageModel) {
        self.message = model
        
        if message.likenum > 0 {
            AttributeLikeStr = getLikeNameWithAttribute()

        }
            setReplayListAtttibuteStr()
    }
    
    
    //回复列表的富文本 同时计算内容的高度
    func setReplayListAtttibuteStr() -> Void {
        //创建一个记录总高度的变量属性
        var rlViewH:CGFloat = 0
        //计算背景视图的宽度
        let rlBgViewW = screenWidth - 3*magin - imageIconW
        //计算文本高度
        let maxSize = CGSize(width: rlBgViewW - 2*magin, height: CGFloat(MAXFLOAT))
        
        //判断是否需要计算点赞文本高度
        if message.likenum > 0 {
            let likeStrSizeH = AttributeLikeStr.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height
            rlViewH += likeStrSizeH + magin * 2 
        }
        
        //先获得回复列表数组
        let replayListArr = message.replaylist as! [FSCReplayListModel]
        //数组
        var attributeArr = [NSAttributedString]()
        //循环转换
        for rlModel in replayListArr {
            //将用户名转换为富文本
            let nameAttribute = NSMutableAttributedString(string: (rlModel.int_name ?? "") + ": ", attributes: [NSForegroundColorAttributeName:UIColor.colorWithHex(hexColor: blueColor),NSFontAttributeName:replayFont])
            
            let contentArributr = NSAttributedString(string: rlModel.int_content ?? "", attributes: [NSForegroundColorAttributeName:UIColor.colorWithHex(hexColor: 0x808080),NSFontAttributeName:replayFont])
            nameAttribute.append(contentArributr)
            attributeArr.append(nameAttribute)
            
            //计算回复内容高度
             let contentStrH = nameAttribute.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
            rlViewH += contentStrH + 6
        }
        
        //添加点赞人名和内容之间的间距
        if attributeArr.count > 0 {
            rlViewH += 5
        }

        //赋值给属性
        replayListAttributeArr = attributeArr
        replayListViewH = rlViewH
    }
    
    //将点赞人名字字符串装换为带点赞图标的富文本字符串
    func getLikeNameWithAttribute() -> NSAttributedString {
        
        //先将图片装换为富文本对象
        let attachment = NSTextAttachment()
        attachment.image = UIImage(named: "notifi_support_press")
        attachment.bounds = CGRect(x: 0, y: -2, width: 12, height: 12)
        let imgAttribute = NSAttributedString(attachment: attachment)
        
        
        //获得点赞字符串
        let likeStr = " " + (message.likelist ?? "")
        //将字符串拼接成富文本
        let likeAttribute = NSAttributedString(string: likeStr)
        
        let attribue = NSMutableAttributedString(attributedString: imgAttribute)
        attribue.append(likeAttribute)
        
        return attribue
    }
}
